package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.controller.vo.InvokeDetailVo;
import test.serverframe.armc.server.manager.domain.Task;
import test.serverframe.armc.server.manager.domain.params.TaskParams;

import java.util.List;

public interface TaskMapper {
    @Delete({
        "delete from tb_ssgp_task",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String taskId);

    @Insert({
        "insert into tb_ssgp_task (task_id, task_name, ",
        "remark, create_time, ",
        "update_time, task_group_id, ",
        "version, is_used, node_arr)",
        "values (#{taskId,jdbcType=VARCHAR}, #{taskName,jdbcType=VARCHAR}, ",
        "#{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
        "#{updateTime,jdbcType=TIMESTAMP}, #{taskGroupId,jdbcType=VARCHAR}, ",
        "#{version,jdbcType=BIGINT}, #{isUsed,jdbcType=BIT}), #{nodeArr,jdbcType=VARCHAR}"
    })
    int insert(Task record);

    @InsertProvider(type=TaskSqlProvider.class, method="insertSelective")
    int insertSelective(Task record);

    @Select({
        "select",
        "task_id, task_name, remark, create_time, update_time, task_group_id, version, ",
        "is_used, node_arr",
        "from tb_ssgp_task",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
        @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="task_group_id", property="taskGroupId", jdbcType=JdbcType.VARCHAR),
        @Result(column="version", property="version", jdbcType=JdbcType.BIGINT),
        @Result(column="node_arr", property="nodeArr", jdbcType = JdbcType.VARCHAR),
        @Result(column="is_used", property="isUsed", jdbcType=JdbcType.BIT)
    })
    Task selectByPrimaryKey(String taskId);

    @UpdateProvider(type=TaskSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Task record);

    @Update({
        "update tb_ssgp_task",
        "set task_name = #{taskName,jdbcType=VARCHAR},",
          "remark = #{remark,jdbcType=VARCHAR},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "task_group_id = #{taskGroupId,jdbcType=VARCHAR},",
          "version = #{version,jdbcType=BIGINT},",
          "is_used = #{isUsed,jdbcType=BIT},",
          "node_arr = #{nodeArr,jdbcType=VARCHAR}",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(Task record);

    @SelectProvider(type = TaskSqlProvider.class, method = "selectByPage")
    @Results({
            @Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "task_group_id", property = "taskGroupId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.BIGINT),
            @Result(column = "node_arr", property = "nodeArr", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_used", property = "isUsed", jdbcType = JdbcType.BIT)
    })
    List<Task> selectByPage(TaskParams params);

	@Select({
		"select *",
		"from (",
			"select tk.task_id,tk.task_name,tk.remark,tk.task_group_id,tei.task_delay",
			"from tb_ssgp_task as tk, tb_ssgp_task_enter_invoke as tei",
			"where tk.task_id = tei.task_id and tei.task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}",
			"order by tk.version desc",
			") as ti",
		"group by ti.task_group_id"
	})
	@Results({
        @Result(column = "task_id", property = "taskId", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_group_id", property = "taskGroupId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_delay", property = "taskDelay", jdbcType = JdbcType.INTEGER),
})
	List<InvokeDetailVo> findTaskByTaskInvokeId(String taskInvokeId);
	
	@Select({
		"select task_name",
		"from tb_ssgp_task"
	})
	List<String> findTaskName();
}
